<?php

namespace ticket\modules\frontend\models;

use Yii;

/**
 * This is the model class for table "tk_order_details".
 *
 * @property int $id
 * @property int $oid
 * @property int $tid
 * @property string $name
 * @property string $phone
 * @property string $certificate
 * @property int $ticket_num
 * @property string $reserve_time
 * @property string $price
 * @property string $valid_time
 * @property string $is_use
 * @property string $verification_code
 * @property string $verification_time
 * @property string $create_time
 */
class OrderDetails extends \yii\db\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'tk_order_details';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['oid', 'tid', 'name', 'phone', 'certificate'], 'required'],
            [['oid', 'tid', 'ticket_num'], 'integer'],
            [['reserve_time', 'valid_time', 'verification_time', 'create_time','isvip'], 'safe'],
            [['price'], 'number'],
            [['is_use'], 'string'],
            [['name'], 'string', 'max' => 16],
            [['phone'], 'string', 'max' => 11],
            [['certificate'], 'string', 'max' => 32],
            [['verification_code'], 'string', 'max' => 255],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'oid' => 'Oid',
            'tid' => 'Tid',
            'name' => 'Name',
            'phone' => 'Phone',
            'certificate' => 'Certificate',
            'ticket_num' => 'Ticket Num',
            'reserve_time' => 'Reserve Time',
            'price' => 'Price',
            'valid_time' => 'Valid Time',
            'is_use' => 'Is Use',
            'verification_code' => 'Verification Code',
            'verification_time' => 'Verification Time',
            'create_time' => 'Create Time',
        ];
    }


    public function getInfo($tid)
    {
        $data = self::find()->where(['tid' => $tid])->asArray()->one();
        return $data;
    }

    public function addDetailsInfo($oid,$params)
    {
        $data = $this->setData($params);
        $data['OrderDetails']['oid'] = $oid;
        if  ($this->load($data) and $this->validate()){
            if ($this->save(0)){
                return true;
            }
        }
        return false;
    }


    public function setData($params)
    {
		//区分是否是购物车
		if($params['postData']['is_list']){
			$data['OrderDetails']['tid'] = 0;
			$data['OrderDetails']['ticket_num'] = $params['all_num'];
		}else{
			$data['OrderDetails']['tid'] = $params['tid'];
			$data['OrderDetails']['ticket_num'] = $params['postData']['num'];
		}
        
        $data['OrderDetails']['name'] = $params['postData']['name'];
        $data['OrderDetails']['phone'] = $params['postData']['phone'];
        $data['OrderDetails']['certificate'] = $params['postData']['certificate'];
        
        $data['OrderDetails']['reserve_time'] = $params['postData']['reserveTime'];
		$data['OrderDetails']['isvip'] = $params['postData']['isvip'];
        $data['OrderDetails']['price'] = $params['rulingPrice'];
//        $data['OrderDetails']['valid_time'] = '';
        $data['OrderDetails']['is_use'] = '2';
//        $data['OrderDetails']['verification_code'] = '';
//        $data['OrderDetails']['verification_time'] = '';
        $data['OrderDetails']['create_time'] = date("Y-m-d H:i:s");
        return $data;
    }



}



